<template>
<j-modal :title="title" :width="width" :visible="visible" :confirmLoading="confirmLoading" switchFullscreen @ok="handleOk" @cancel="handleCancel" cancelText="关闭">
    <a-spin :spinning="confirmLoading">
        <a-form :form="form">
            <a-row>
                <a-col :span="12">
                    <a-form-item label="货品名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <j-dict-select-search type="list" v-decorator="['itemName',validatorRules.itemName]" :trigger-change="true" dictCode="bas_material,name,id" @sendNameId="getNameId" />
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="货品编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input readOnly v-model="dataList.itemNumber" placeholder="请输入货品编号"></a-input>
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="规格" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input readOnly v-model="dataList.specifications" placeholder="请输入规格"></a-input>
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="单位" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input readOnly v-model="dataList.unit" placeholder="请输入单位"></a-input>
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item :required="true" label="数量" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input-number :min="1" v-model="dataList.number" placeholder="请输入数量" style="width: 100%" />
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="单价" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input readOnly v-model="dataList.unitPrice" placeholder="请输入单价"></a-input>
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="税率" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input readOnly v-model="dataList.rate" placeholder="请输入税率"></a-input>
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="贷款" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input v-decorator="['loan']" placeholder="请输入贷款"></a-input>
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="税额" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input readOnly v-model="tax" placeholder="请输入税额"></a-input>
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="金额" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input readOnly v-model="money" placeholder="请输入金额"></a-input>
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="产品批号" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input v-decorator="['productBatchNumber']" placeholder="请输入产品批号"></a-input>
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input v-decorator="['note']" placeholder="请输入备注"></a-input>
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="附项" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input v-decorator="['attachedItems']" placeholder="请输入附项"></a-input>
                    </a-form-item>
                </a-col>
            </a-row>
        </a-form>
    </a-spin>
</j-modal>
</template>

<script>
import {
    httpAction,
    getGoodsInfo
} from '@/api/manage'
import pick from 'lodash.pick'

export default {
    name: 'PurchaseOrderSubtableModal',
    components: {},
    props: {
        mainId: {
            type: String,
            required: false,
            default: '',
        },
    },
    data() {
        return {
            form: this.$form.createForm(this),
            title: '操作',
            width: 800,
            dataList: {},
            visible: false,
            model: {},
            labelCol: {
                sm: {
                    span: 6,
                },
            },
            wrapperCol: {
                sm: {
                    span: 16,
                },
            },
            confirmLoading: false,
            validatorRules: {
                itemName: {
                    rules: [{
                        required: true,
                        message: '请选择货品名称!'
                    }]
                },
            },
            url: {
                add: '/procurement/purchaseOrder/addPurchaseOrderSubtable',
                edit: '/procurement/purchaseOrder/editPurchaseOrderSubtable',
            },
        }
    },
    created() {},
    computed: {
        money() {
            if (this.dataList.number && this.dataList.unitPrice) {
                return this.dataList.number * this.dataList.unitPrice + ''
            }
        },
        tax() {
            if (this.dataList.rate && this.money) {
                return ((this.dataList.rate.split('%')[0] / 100) * this.money).toFixed(2) + ''
            }
        },
    },
    methods: {
        //选择货品后请求货品数据并进行响应式赋值
        async getNameId(value) {
            this.dataList.itemId = value
            let result = await getGoodsInfo({
                params: {
                    id: value,
                },
            })
            let {
                dataList
            } = this;
            let {
                unit,
                unitId,
                code,
                salePrice,
                model
            } = result.result
            this.$set(dataList, 'unit', unit)
            this.$set(dataList, 'unitId', unitId)
            this.$set(dataList, 'itemNumber', code)
            this.$set(dataList, 'unitPrice', salePrice)
            this.$set(dataList, 'specifications', model)
        },
        getName(value) {
            this.$set(this.dataList, 'unit', value)
        },
        add() {
            this.edit({})
        },
        edit(record) {
            this.form.resetFields()
            this.dataList = {
                rate: '13%',
                unit: record.unit,
                unitId: record.unitId,
                itemId: record.itemId,
                number: record.number,
                itemNumber: record.itemNumber,
                unitPrice: record.unitPrice,
                specifications: record.specifications,
            }
            this.model = Object.assign({}, record)
            this.visible = true
            this.$nextTick(() => {
                this.form.setFieldsValue(pick(this.model, 'itemName', 'loan', 'productBatchNumber', 'note', 'attachedItems'))
            })
        },
        close() {
            this.$emit('close')
            this.visible = false
        },
        handleOk() {
            const that = this
            if (this.dataList.number == '' || this.dataList.number == undefined) {
                this.$message.warning('请输入数量!')
                return false;
            }
            // 触发表单验证
            this.form.validateFields((err, values) => {
                if (!err) {
                    that.confirmLoading = true
                    let httpurl = ''
                    let method = ''
                    if (!this.model.id) {
                        httpurl += this.url.add
                        method = 'post'
                    } else {
                        httpurl += this.url.edit
                        method = 'put'
                    }
                    this.dataList.money = this.money
                    this.dataList.tax = this.tax
                    values = Object.assign(values, this.dataList)
                    let formData = Object.assign(this.model, values)
                    formData['purchaseOrderId'] = this.mainId
                    httpAction(httpurl, formData, method)
                        .then((res) => {
                            if (res.success) {
                                that.$message.success(res.message)
                                that.$emit('ok')
                            } else {
                                that.$message.warning(res.message)
                            }
                        })
                        .finally(() => {
                            that.confirmLoading = false
                            that.close()
                        })
                }
            })
        },
        handleCancel() {
            this.close()
        },
        popupCallback(row) {
            this.form.setFieldsValue(pick(row, 'itemName', 'loan', 'productBatchNumber', 'note', 'attachedItems'))
        },
    },
}
</script>
